package com.migraine;

import com.migraine.model.Users;
import com.migraine.service.UsersService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthorizingRealm {

    @Autowired
    private UsersService usersService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        // 用户登录密码匹配
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        String username = userToken.getUsername();
        Users user = new Users();
        if (!username.equals("")) {
            user = usersService.getUsers(username);
            if (user == null) {
                return null;
            }
        }
        String pwd = user.getPassword();
        return new SimpleAuthenticationInfo("", pwd, "");
    }
}